﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Amib.Threading;
using Empires.Tool.Framework;
using Empires.Biz.Configuration;

namespace Empires.Infrastructure.Component.WorkPool
{
    public static class WorkPool
    {
        static volatile SmartThreadPool pool = null;
        static volatile Dictionary<GroupPool, IWorkItemsGroup> Group;
        static WorkPool()
        {
            int minsize = ConfigReader.WorkPool.MinThreadSize;
            int maxsize = ConfigReader.WorkPool.MaxThreadSize;
            try
            {
                pool = new SmartThreadPool(10 * 1000, maxsize, minsize);
                Group = new Dictionary<GroupPool, IWorkItemsGroup>();
                
                Group.Add(GroupPool.EmailGroup,
                    pool.CreateWorkItemsGroup(ConfigReader.WorkPool.EmailQueue.ThreadGroupSize));
                
                Group.Add(GroupPool.CommonWork,
                    pool.CreateWorkItemsGroup(ConfigReader.WorkPool.CommonWorkQueue.ThreadGroupSize));
                
            }
            catch (Exception e)
            {
                LogHelper.Write(CommonLogger.ExServer, LogLevel.Error, e);
                throw e;
            }
                
        }

        public static void Run() { }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="workType"></param>
        /// <param name="context"></param>
        public static void Append<T>(WorkType workType, T context, System.Action callback = null)
        {
            var workItem = new WorkItem<T>(workType).Build(context);
            if (workItem.IsDo) //如果被要求执行
            {
                AddNewTask(workType, workItem.Action, workItem.Context, callback);
            }
        }
        private static void AddNewTask(WorkType workType, Action<object> doAction, object context, System.Action callback)
        {
            var iworkitem = workType.GetWorkPool()
                .QueueWorkItem(doAction, context);
            if (callback != null)
            {
                while (!iworkitem.IsCompleted)
                {
                    Thread.Sleep(5);
                }
                callback.Invoke();
            }
            //ThreadPool.UnsafeQueueUserWorkItem(callback, context);
        }
        private static IWorkItemsGroup GetWorkPool(this WorkType workType)
        {
            switch (workType)
            {
                default:
                case WorkType.email_for_normalnotify:
                //case WorkType.email_for_register:
                //case WorkType.email_for_resetpassword:
                //case WorkType.email_for_welcomeExpert:
                //case WorkType.email_for_invitereviser_notity:
                //case WorkType.email_for_inviteexpert_notity:
                //case WorkType.email_for_jobnotice:
                //case WorkType.email_for_manuscriptcheck:
                //case WorkType.email_for_examinejobdistribution:
                //case WorkType.email_for_urgeexamine:
                    return Group[GroupPool.EmailGroup];
            }
        }
    }
    public enum WorkType
    {
        /// <summary>
        /// 普通通知邮件
        /// </summary>
        email_for_normalnotify,
        ///// <summary>
        ///// 为注册发送邮箱认证邮件
        ///// </summary>
        //email_for_register,
        ///// <summary>
        ///// 为重置密码发送重置确认邮件
        ///// </summary>
        //email_for_resetpassword,
        ///// <summary>
        ///// 邀请专家加入编委会
        ///// </summary>
        //email_for_inviteexpert_notity,
        //email_for_welcomeExpert,
        ///// <summary>
        ///// 邀请内会员加入初审员
        ///// </summary>
        //email_for_invitereviser_notity,
        ///// <summary>
        ///// 任务通知
        ///// </summary>
        //email_for_jobnotice,
        ///// <summary>
        ///// 初审通知邮件
        ///// </summary>
        //email_for_manuscriptcheck,
        ///// <summary>
        ///// 审稿任务分发
        ///// </summary>
        //email_for_examinejobdistribution,
        ///// <summary>
        ///// 催审邮件
        ///// </summary>
        //email_for_urgeexamine,
        ///// <summary>
        ///// 终审完成录用，催收出版费
        ///// </summary>
        //email_for_publication
    }
    public enum GroupPool
    { 
        /// <summary>
        /// 邮件线程组
        /// </summary>
        EmailGroup,
        /// <summary>
        /// 公共线程组
        /// </summary>
        CommonWork
    }
}
